/****************************************************
    Author:            龙之介
    CreatTime:    2021/2/12 14:4:55
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using System.Linq;



namespace LongZhiJie
{
	public class InitManger :MonoBehaviour
	{
        public static InitManger Instance;
        public GameObject Lock;
        public bool CanMove=true;
        public GameObject[] A;
        public GameObject[] D;
        public GameObject[] W;
        public GameObject[] S;
        public GameObject[] E;
        public GameObject[] Q;
        public Sprite[] sp;//ADWS
        private Dictionary<Dir, GameObject[]> DirGameobject;
        private void Awake()
        {
            Instance = this;

            DirGameobject = new Dictionary<Dir, GameObject[]>();

            DirGameobject.Add(Dir.A, A);
            DirGameobject.Add(Dir.D, D);
            DirGameobject.Add(Dir.W, W);
            DirGameobject.Add(Dir.S, S);
            DirGameobject.Add(Dir.Q, Q);
            DirGameobject.Add(Dir.E, E);
        }
        #region 起飞
        float x, y;
        private void Update()
        {
            
            if (Input.GetKey(KeyCode.A)&&A[1].transform.GetComponent<SpriteRenderer>().color.a==1)
            {
                OnPress(Dir.A);
            }
            if (Input.GetKey(KeyCode.S) && S[1].transform.GetComponent<SpriteRenderer>().color.a == 1)
            {
       
                OnPress(Dir.S);
            }
            if (Input.GetKey(KeyCode.W) && W[0].transform.GetComponent<SpriteRenderer>().color.a == 1)
            {
               // Debug.LogError(W[1].transform.GetComponent<SpriteRenderer>().color.a);
                OnPress(Dir.W);
            }
            if (Input.GetKey(KeyCode.D) && D[1].transform.GetComponent<SpriteRenderer>().color.a == 1)
            {
                OnPress(Dir.D);
            }
            if (Input.GetKeyUp(KeyCode.A))
            {
                OnUp(Dir.A);
            }
            if (Input.GetKeyUp(KeyCode.S))
            {
                OnUp(Dir.S);
            }
            if (Input.GetKeyUp(KeyCode.W))
            {
                OnUp(Dir.W);
            }
            if (Input.GetKeyUp(KeyCode.D))
            {
                OnUp(Dir.D);
            }


            // Debug.LogError(1);
            if (!Lock.activeSelf)
            {
                InitManger.Instance.CanMove = true;
            }
            else
            {
                InitManger.Instance.CanMove = false;
            }
        }
        float __暗化=0.38f;
        public void CanNotPress(Dir dir)
        {
            
            ChangeSR(__暗化, dir);
        }
        public void CanPress(Dir dir)
        {
            ChangeSR(1, dir);
        }
        private void ChangeSR(float _暗化,Dir dir)
        {
            switch (dir)
            {
                case Dir.Vertical:

                    CHangeSRa(_暗化, W);
                    Debug.LogError(W[1] + " " + W[1].transform.GetComponent<SpriteRenderer>().color);
                    Debug.LogError(W[0] + " " + W[0].transform.GetComponent<SpriteRenderer>().color);
                    CHangeSRa(_暗化, S);
                    break;
                case Dir.Herizatal:
                    CHangeSRa(_暗化, D);

                    CHangeSRa(_暗化, A);
                    ; break;
                case Dir.Q:Debug.LogError(_暗化); CHangeSRa(_暗化, Q);
                    break;
                case Dir.E:
                    CHangeSRa(_暗化, E);
                    break;

            }
        }
        private void CHangeSRa(float a,GameObject[] 需要改变的物体)
        {

            foreach (GameObject As in 需要改变的物体)
            {
                As.transform.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, a);
            }
        }

        private void ChangeSP(Dir s,GameObject[] 需要改变的物体)
        {

            foreach (GameObject As in 需要改变的物体)
            {
                As.transform.GetComponent<SpriteRenderer>().sprite = sp[(int)s ];
            }
        }
        private void OnPress(Dir s)
        {
            if(DirGameobject.ContainsKey(s))
            {
                ChangeSP(Dir.Press, DirGameobject[s]);
            }
            else
            {
                Debug.LogError("not find this" + s);
            }
        }
        private void OnUp(Dir s)
        {
            if (DirGameobject.ContainsKey(s))
            {
                ChangeSP(s, DirGameobject[s]);
            }

            else
            {
                Debug.LogError("not find this" + s);
            }
        }
        public enum Dir
        {
            A,
            D,
            W,
            S,
            Press,
            E,
            Q,
            Vertical,
            Herizatal,
            ALL,
            ChangeGun,
        }
        #endregion

        public void CloseAnimators()
        {
            foreach(GameObject ani in A)
            {
                ani.transform.GetComponent<Animator>().enabled = false;
            }
            foreach (GameObject ani in W)
            {
                ani.transform.GetComponent<Animator>().enabled = false;
            }
            foreach (GameObject ani in D)
            {
                ani.transform.GetComponent<Animator>().enabled = false;
            }
            foreach (GameObject ani in S)
            {
                ani.transform.GetComponent<Animator>().enabled = false;
            }
            foreach(GameObject ani in Q)
            {
                ani.transform.GetComponent<Animator>().enabled = false;

            }

         
        }
    }
}